<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="bbs.*, core.*, util.*" %>
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/WEB-INF/include/header.jsp" %>
<style>
    div.form-inline { margin-bottom: 5px; }
    select[name=sz] { width: 60px; }
    select[name=od] { width: 100px; }
    select[name=ss] { width: 100px; margin-left: 100px; }
    td:first-child, th:first-child { width: 40px; text-align: center; }
    td:nth-child(3) { width: 100px; }
    td:nth-child(4) { width: 80px; }
    td:nth-child(5) { width: 50px; text-align: center; }
    tbody tr:hover { background-color: #FFC; cursor: pointer; }
    small { color: #aaa; }
    img { display: inline-block; }
</style>
<script>
$(function() {
    $("select[name=ct], select[name=sz]").change(function() {
        $("form").submit();
    })
    $("tbody tr").click(function() {
        location.href = "article.jsp" + location.search + "&aid=" + $(this).attr("data-id");
    })
    $("div.pagination a").click(function() {
        $("input[name=pg]").val($(this).attr("data-page"));
        $("form").submit();
    })
})
</script>
</head>
<%
    RequestParameter param = new RequestParameter(request);
    int boardId = param.getInt("bid", 0);
    int currentPage = param.getInt("pg", 1);
    int pageSize = param.getInt("sz", 15);
    int srchType = param.getInt("ss", 0);
    String srchText = param.getString("st", "");
    int category = param.getInt("ct", 0);
    String cmd = param.getString("cmd", "");
    String errMsg = null;

    BoardService boardService = new BoardService(request, boardId);
    if (boardService.canListArticles() == false)
        new UnauthorizedAccessException("게시글 목록 보기 권한 없음");

    ArrayList<ArticleCategory> categories = ArticleCategoryDAO.selectAll(boardId);
    ArrayList<Article> list1 = ArticleDAO.selectNotice(boardId, boardService.getCurrentUserId());
    ArrayList<Article> list2 = ArticleDAO.selectPage(boardId, boardService.getCurrentUserId(),
            currentPage, pageSize, srchType, srchText, category);

    int recordCount = ArticleDAO.getRecordCount(boardId, srchType, srchText, category);
    int pageCount = (recordCount + pageSize - 1 ) / pageSize;
    int basePage = ((currentPage - 1) / 10) * 10;

    String urlCreate = "articleCreate.jsp?" + request.getQueryString();
    String urlBoardEdit = "boardEdit.jsp?" + request.getQueryString();
    String ROOT = request.getContextPath();
%>
<body>
<%@ include file="/WEB-INF/include/menu.jsp" %>

<div class="container">

    <h1><%= boardService.getBoard().getTitle() %></h1>
    <hr />

    <form>
        <input type="hidden" name="bid" value="<%= boardId %>" />
        <input type="hidden" name="pg" value="1" />

        <div class="pull-right">
            <% if (boardService.canManageBoard()) { %>
                <a href="<%= urlBoardEdit %>" class="btn btn-small">
                    <i class="icon icon-cog"></i> 게시판 설정 관리</a>
            <% } %>
            <% if (boardService.canCreateArticle()) { %>
                <a href="<%= urlCreate %>" class="btn btn-small btn-primary">
                    <i class="icon icon-pencil icon-white"></i> 글쓰기</a>
            <% } %>
        </div>

        <div class="form-inline">
            분류:
            <select name="ct">
                <option value="0">전체</option>
                <% for (ArticleCategory ac : categories) { %>
                    <option value="<%= ac.getId() %>" <%= category==ac.getId() ? "selected" : "" %>>
                           <%= ac.getName() %></option>
                <% } %>
            </select>

            <select name="ss">
                <option value="0" <%= srchType==0 ? "selected" : "" %>>검색없음</option>
                <option value="1" <%= srchType==1 ? "selected" : "" %>>제목</option>
                <option value="2" <%= srchType==2 ? "selected" : "" %>>사용자</option>
                <option value="3" <%= srchType==3 ? "selected" : "" %>>본문</option>
            </select>

            <input type="text" name="st" value="<%= srchText %>" />
            <button type="submit" class="btn btn-small search">검색</button>
        </div>

        <table class="table table-condensed table-bordered">
            <thead>
                <tr>
                    <th>no</th>
                    <th>제목</th>
                    <th>작성자</th>
                    <th>작성일</th>
                    <th>조회수</th>
                </tr>
            </thead>
            <tbody>
                <% for (Article article : list1) { %>
                    <tr data-id="<%=article.getId()%>">
                        <td><span class="label label-important">공지</span></td>
                        <td><small><%= article.getCategory() %>:</small> <%= article.getTitle() %></td>
                        <td><%= article.getName() %></td>
                        <td><%= DateUtil.toYMD(article.getWriteTime()) %></td>
                        <td><%= article.getTotalReadCount() %></td>
                    </tr>
                <% } %>
                <% for (Article article : list2) { %>
                    <tr data-id="<%=article.getId()%>">
                        <td><%= article.getNo() %></td>
                        <td><% if (article.getIndentation() > 0) { %>
                                <img style="margin-left: <%= 10 * article.getIndentation() %>px"
                                     src="<%=ROOT%>/res/images/reply.png" />
                            <% } %>
                            <small><%= article.getCategory() %>:</small>
                            <%= article.getTitle() %>
                            <% if (article.getCommentCount() > 0) { %>
                                <small>[<%= article.getCommentCount() %>]</small>
                            <% } %>
                            <% if (article.getMyReadCount() == 0) { %>
                                <img src="<%=ROOT%>/res/images/new.png" />
                            <% } %>
                        </td>
                        <td><%= article.getName() %></td>
                        <td><%= DateUtil.toYMD(article.getWriteTime()) %></td>
                        <td><%= article.getTotalReadCount() %></td>
                    </tr>
                <% } %>
            </tbody>
        </table>

        <div class="controls pull-right form-inline">
            화면크기:
            <select name="sz">
                <option <%= pageSize==10 ? "selected" : "" %>>10</option>
                <option <%= pageSize==15 ? "selected" : "" %>>15</option>
                <option <%= pageSize==30 ? "selected" : "" %>>30</option>
                <option <%= pageSize==60 ? "selected" : "" %>>60</option>
                <option <%= pageSize==100 ? "selected" : "" %>>100</option>
            </select>
        </div>

        <div class="pagination pagination-small pagination-centered">
            <ul>
                <% if (basePage > 0) { %>
                    <li><a data-page="<%= basePage %>">Prev</a></li>
                <% } %>
                <% for (int i=1; i <= 10 && basePage + i <= pageCount; ++i) { %>
                    <li class='<%= basePage+i==currentPage ? "active" : "" %>'>
                        <a data-page="<%= basePage+i %>"><%= basePage + i %></a></li>
                <% } %>
                <% if (basePage + 11 <= pageCount) { %>
                    <li><a data-page="<%= basePage+11 %>">Next</a></li>
                <% } %>
            </ul>
        </div>

    </form>

</div>

<%@ include file="/WEB-INF/include/footer.jsp" %>
</body>
</html>
